package com.hs.shirodemo2.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hs.shirodemo2.constant.CommonConstant;
import com.hs.shirodemo2.entity.SysPermission;
import com.hs.shirodemo2.mapper.SysPermissionMapper;
import com.hs.shirodemo2.service.SysPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysPermissionServiceImpl implements SysPermissionService {

    @Autowired
    private SysPermissionMapper sysPermissionMapper;

    @Override
    public void add(SysPermission entity) {
        sysPermissionMapper.insert(entity);
    }

    @Override
    public void edit(SysPermission entity) {
        sysPermissionMapper.update(entity);
    }

    @Override
    public void remove(Integer id) {
        sysPermissionMapper.delete(id);
    }

    @Override
    public void removeBatch(Integer[] ids) {
        sysPermissionMapper.deleteBatch(ids);
    }

    @Override
    public List<SysPermission> find(SysPermission entity) {
        return sysPermissionMapper.select(entity);
    }

    @Override
    public Map<String, Object> findPage(Integer pageNum, Integer pageSize, SysPermission entity) {
        //分页业务
        PageHelper.startPage(pageNum, pageSize);
        List<SysPermission> list = sysPermissionMapper.select(entity);
        PageInfo<SysPermission> pageInfo = new PageInfo<>(list);
        //1.当前页面数据（数据列表）2.此次查询的总记录数
        Map<String, Object> pageMap = new HashMap<>();
        pageMap.put(CommonConstant.PAGE_LIST, list);
        pageMap.put(CommonConstant.PAGE_TOTAL, pageInfo.getTotal());
        return pageMap;
    }

    @Override
    public SysPermission findById(Integer id) {
        return sysPermissionMapper.selectById(id);
    }

    @Override
    public List<SysPermission> selectPermissionMenuAndList() {
        return sysPermissionMapper.selectPermissionMenuAndList();
    }

    @Override
    public List<SysPermission> findMenu(Integer userId) {
        return sysPermissionMapper.selectMenuPermissionByUserId(userId);
    }

    @Override
    public List<SysPermission> findPermissionByUserId(Integer userId) {
        return sysPermissionMapper.selectAllPermissionByUserId(userId);
    }


}